Results 1 to 2 of 2

Thread: Using signal slots mechanism for displaying a transient “generating image” dialog

  1. #1
    Join Date
    Feb 2014
    Posts
    11
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Using signal slots mechanism for displaying a transient “generating image” dialog

    want to display a "generating image..." kind of modal dialog, other than the main GUI. This "generating image..." dialog should be temporary, and be displayed and disappear without user intervention.

    For displaying this dialog, the Qt code should check for existence of a .txt file in a specific location in the PC's hard disk. If the .txt file exists, then the dialog should pop-up.

    For making this dialog disappear, the Qt code should check whether that .txt file contains the string "OK" in the first line. The dialog should disappear only when this "OK" is found, until then it should continue to display "generating image..."

    A good way to do this is to use signal slot mechanism. I would like to know, what functions should be used as SIGNALS in both the cases, of displaying and removing the dialog.

    So far, I could manage a simple code, illustrating a "generating image..." using signal slot mechanism, but with setValue() and pressing a push button(i.e. involving user intervention), and not with the checking of .txt file or the "OK" string inside that .txt file(user non-intervention).

    Please advise me, whether my logic can be implemented? If yes, how? Also, what SIGNALs should be used?

    Here is my code:

    dialog.h
    Qt Code:
    1. #ifndef DIALOG_H
    2. #define DIALOG_H
    3.  
    4. #include <QDialog>
    5. #include <QProgressDialog>
    6.  
    7. #include "mytask.h"
    8.  
    9. namespace Ui {
    10. class Dialog;
    11. }
    12.  
    13. class Dialog : public QDialog
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. explicit Dialog(QWidget *parent = 0);
    19. ~Dialog();
    20.  
    21. private slots:
    22. void on_modalButton_clicked();
    23.  
    24. private:
    25. Ui::Dialog *ui;
    26. MyTask *myTask;
    27. };
    28.  
    29. #endif // DIALOG_H
    To copy to clipboard, switch view to plain text mode 

    mytask.h
    Qt Code:
    1. #ifndef MYTASK_H
    2. #define MYTASK_H
    3.  
    4. #include <QObject>
    5. #include <QProgressDialog>
    6. #include <QTimer>
    7. #include <QDebug>
    8.  
    9. #define NUM_TASKS 50000
    10.  
    11. class MyTask : public QObject
    12. {
    13. Q_OBJECT
    14. public:
    15. explicit MyTask(QObject *parent = 0);
    16.  
    17. signals:
    18.  
    19. public slots:
    20. void perform();
    21. void cancel();
    22.  
    23. private:
    24. int steps;
    25. QTimer *t;
    26. };
    27.  
    28. #endif // MYTASK_H
    To copy to clipboard, switch view to plain text mode 

    dialog.cpp
    Qt Code:
    1. #include "dialog.h"
    2. #include "ui_dialog.h"
    3.  
    4. Dialog::Dialog(QWidget *parent) :
    5. QDialog(parent),
    6. ui(new Ui::Dialog)
    7. {
    8. ui->setupUi(this);
    9. }
    10.  
    11. Dialog::~Dialog()
    12. {
    13. delete ui;
    14. }
    15.  
    16. void Dialog::on_modalButton_clicked()
    17. {
    18. myTask = new MyTask;
    19. }
    To copy to clipboard, switch view to plain text mode 

    mytask.cpp
    Qt Code:
    1. #include "mytask.h"
    2.  
    3. MyTask::MyTask(QObject *parent) :
    4. QObject(parent), steps(0)
    5. {
    6. pd = new QProgressDialog("generating image...", 0, 0, NUM_TASKS);
    7. connect(pd, SIGNAL(canceled()), this, SLOT(cancel()));
    8.  
    9. t = new QTimer(this);
    10. connect(t, SIGNAL(timeout()), this, SLOT(perform()));
    11. t->start(0);
    12.  
    13. /* to make a window modal in nature. */
    14. pd->setModal(true);
    15.  
    16. }
    17.  
    18. void MyTask::perform()
    19. {
    20. disconnect(pd, SIGNAL(canceled()), this, SLOT(cancel()));
    21. pd->setValue(steps); //... perform one percent of the operation
    22. steps++;
    23. qDebug()<<steps;
    24. if (steps > pd->maximum())
    25. {
    26. t->stop();
    27. }
    28. }
    29.  
    30. void MyTask::cancel()
    31. {
    32. t->stop(); //... cleanup
    33. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include "dialog.h"
    2. #include <QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. Dialog w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Using signal slots mechanism for displaying a transient “generating image” dialog

    For making this dialog disappear, the Qt code should check whether that .txt file contains the string "OK" in the first line
    You can use QFileSystemWatcher for that (fileChanged(QString) signal).
    The dialog should disappear only when this "OK" is found, until then it should continue to display "generating image..."
    I'd use some kind of timeout too, especially if this "OK" is written by some other process. Btw. check if there is no "OK" already before displaying the dialog
    Also, I don't really understand this line:
    Qt Code:
    1. void MyTask::perform()
    2. {
    3. disconnect(pd, SIGNAL(canceled()), this, SLOT(cancel()));
    4. ...
    5. }
    To copy to clipboard, switch view to plain text mode 
    Why do you keep disconnecting the signal over and over on each timer tick ?

Similar Threads

  1. signal/slot mechanism and shared library
    By mickael in forum Qt Programming
    Replies: 5
    Last Post: 25th November 2012, 14:08
  2. signal -slot mechanism
    By qt_user in forum Qt Programming
    Replies: 2
    Last Post: 30th July 2010, 18:34
  3. When should I use signal/slot mechanism
    By Bryku in forum Newbie
    Replies: 3
    Last Post: 10th February 2010, 22:24
  4. slow signal-slot-mechanism
    By blm in forum Qt Programming
    Replies: 11
    Last Post: 28th October 2008, 17:10
  5. The threaded signal/slot mechanism
    By xbtl in forum Newbie
    Replies: 1
    Last Post: 30th March 2008, 00:07

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.